package isec.amov.tp2.automaintenance;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
//import android.content.Context;
import android.content.DialogInterface;
//import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class DetalheManual extends Activity {

	private int pos, id;
	private double km;
	/*private double oleo;
	private int dia, mes, ano;
	private double filtro;
	private double pastilhas;*/
	private DBAdapter db;
	//private Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detalhe_manual);
		
		Bundle bundle = this.getIntent().getExtras();
		pos = bundle.getInt("id");
		km = bundle.getDouble("quilometros");
		Log.i("teste id", "id " + pos);
		Log.i("teste km", "km " + km);
		
		db = new DBAdapter(this);
		db.open(this);
		
		//context = this;
		
		/*Log.i("teste", "det1");
		context = this;
		
		Bundle bundle = this.getIntent().getExtras();
		pos = bundle.getInt("id");
		
		Log.i("teste", "det2");
		
		db = new DBAdapter(this);
		db.open(this);
		
		Log.i("teste", "det3");
		
		//Detalhe det = db.getDetalhePos(pos);
		Log.i("teste", "det4");
		//id = det.getId();
		Log.i("teste", "det4.5");
		
		
		EditText oleo = (EditText) findViewById(R.id.editOleo);
		oleo.setText(det.getOleo() + "");
		
		DatePicker dp = (DatePicker) findViewById(R.id.dateInspeccao);
		dp.updateDate(det.getAno(), det.getMes(), det.getDia());
		
		EditText filtro = (EditText) findViewById(R.id.editFiltro);
		filtro.setText(det.getFiltroMotor() + "");
		
		EditText pastilhas = (EditText) findViewById(R.id.editPastilhas);
		pastilhas.setText(det.getPastilhas() + "");
		
		Log.i("teste", "det5");*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_detalhe_manual, menu);
		return true;
	}
	
	public void onCancelar(View v)
	{
		db.deleteCarro(pos);
		db.deleteDetalhe(pos);
		db.close();
		Toast.makeText(this, R.string.carro_cancelado, Toast.LENGTH_LONG).show();
		finish();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		db.deleteCarro(pos);
		db.deleteDetalhe(pos);
		db.close();
		Toast.makeText(this, R.string.carro_cancelado, Toast.LENGTH_LONG).show();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	/*@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		db.deleteCarro(id);
		db.deleteDetalhe(id);
		db.close();
		Toast.makeText(this, R.string.carro_cancelado, Toast.LENGTH_LONG).show();
	}*/
	
	public void onGuardar(View v){
		
		double oleo = -1;
		double filtro = -1;
		double pastilhas = -1;
		int ano = -1, mes = -1, dia = -1;
		AlertDialog.Builder bdialog;
		
		try{
			EditText novoOleo = (EditText) findViewById(R.id.editOleo);
			oleo = Double.parseDouble(novoOleo.getText().toString());
			oleo += km;
			Log.i("teste", "teste oleo " + oleo);
			
			DatePicker dp = (DatePicker) findViewById(R.id.dateInspeccao);
			ano = dp.getYear();
			mes = dp.getMonth() + 1;
			dia = dp.getDayOfMonth();
			
			EditText novoFiltro = (EditText) findViewById(R.id.editFiltro);
			filtro = Double.parseDouble(novoFiltro.getText().toString());
			filtro += km;
			
			EditText novoPastilhas = (EditText) findViewById(R.id.editPastilhas);
			pastilhas = Double.parseDouble(novoPastilhas.getText().toString());
			pastilhas += km;
			
			bdialog = new AlertDialog.Builder(this);
			if (!checkValues(dia, mes, ano)) {
				bdialog.setTitle("");
				bdialog.setMessage(R.string.invalidData);
				bdialog.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				AlertDialog dialog = bdialog.create();
				dialog.show();
				
		}else {
			Detalhe det = new Detalhe(pos, oleo, ano,
					mes, dia, filtro, pastilhas);
			//Log.i("teste", det.toString() + "");
			db.criaDetalhe(det);

			bdialog.setTitle("");
			bdialog.setMessage(R.string.novo_carro_sucesso);
			bdialog.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							db.close();
							/*Intent intent=new Intent(context, ListarCarros.class);
							startActivity(intent);*/
							finish();
						}
					});
			AlertDialog dialog = bdialog.create();
			dialog.show();
		}
	} catch (Exception e) {
	} finally {
	}

	}
	
	public boolean checkValues(int dia, int mes, int ano) {

		Calendar calendar = Calendar.getInstance();

		int currentYear = calendar.get(Calendar.YEAR);
		if (ano < currentYear || ano > Detalhe.maxYear) {
			return false;
		}

		if (mes < 1 || mes > 12) {
			return false;
		}

		int maxDay = -1;
		if (ano != -1 && mes != -1) {
			calendar.set(ano, mes, 1);
			maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

			if (dia < 1 || dia > maxDay) {
				return false;
			}
		}
		return true;
	}


	
}
